package org.jeecg.glr.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.glr.entity.GlrLightPole;
import org.jeecg.glr.entity.GlrRadio;
import org.jeecg.glr.mapper.GlrLightPoleMapper;
import org.jeecg.glr.mapper.GlrVideoYsMapper;
import org.jeecg.glr.service.IGlrLightPoleService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 路灯灯杆管理
 * @Author: jeecg-boot
 * @Date: 2020-03-05
 * @Version: V1.0
 */
@Service
public class GlrLightPoleServiceImpl extends ServiceImpl<GlrLightPoleMapper, GlrLightPole> implements IGlrLightPoleService {

    @Resource
    GlrLightPoleMapper glrLightPoleMapper;

    @Override
    public void updateAndSynchronizationGateWayById(GlrLightPole glrLightPole) {
        if (StringUtil.isEmpty(glrLightPole.getLatitude())) {
            glrLightPole.setLatitude(GLRConstant.GLR_LATITUDE);
        }
        if (StringUtil.isEmpty(glrLightPole.getLongitude())) {
            glrLightPole.setLongitude(GLRConstant.GLR_LONGITUDE);
        }
       // updateGatewayLightPoleCode(glrLightPole);
        glrLightPoleMapper.updateById(glrLightPole);
    }

    public int getMaxCodeByProjectCode(String projectCode) {
        return glrLightPoleMapper.getMaxCodeByProjectCode(projectCode);
    }

    @Override
    public void saveAndSynchronizationGateWay(GlrLightPole glrLightPole) {
        if(glrLightPole.getPoleNum()!=null){
            List<GlrLightPole> glrList = new ArrayList<>();
            int num=glrLightPole.getPoleNum();
            int code = getMaxCodeByProjectCode(glrLightPole.getProjectCode());
            for(int i=0;i<num;i++){
                GlrLightPole glr = new GlrLightPole();
                code++;
                String lg_code = String.format("%04d", code);
                glr.setCode(lg_code);
                if (StringUtil.isEmpty(glrLightPole.getLatitude())) {
                    glr.setLatitude(GLRConstant.GLR_LATITUDE);
                }
                if (StringUtil.isEmpty(glrLightPole.getLongitude())) {
                    glr.setLongitude(GLRConstant.GLR_LONGITUDE);
                }
                glr.setProjectCode(glrLightPole.getProjectCode());
                //glr.setIsWisdom(glrLightPole.getIsWisdom());
                glr.setLightPoleType(glrLightPole.getLightPoleType());

                glr.setMemo(glrLightPole.getMemo());

                glr.setVideoYn(GLRConstant.YN_N);
                glr.setRadioYn(GLRConstant.YN_N);
                glr.setGatewayYn(GLRConstant.YN_N);
                glr.setLedYn(GLRConstant.YN_N);
                glr.setSensorYn(GLRConstant.YN_N);
                glr.setLightYn(GLRConstant.YN_N);

                glrList.add(glr);
            }
            super.saveBatch(glrList);
        }
    }

    @Override
    public GlrLightPole getFirstPoleByProjectCode(String projectCode){
        String stsql="select * from glr_light_pole where project_code='"+projectCode+"' and longitude is not null and latitude is not null LIMIT 1";
        GlrLightPole glrLightPole=glrLightPoleMapper.getGlrLightPoleSelect(stsql);
        return glrLightPole;
    }

    @Override
    public void updateLgAll(GlrLightPole glrLightPole){
        String projectCode="";
        if(StringUtil.isNotEmpty(glrLightPole.getProjectCode())){
            projectCode=glrLightPole.getProjectCode();
        }else{
            return;
        }
        String latitude="";
        if(StringUtil.isNotEmpty(glrLightPole.getLatitude())){
            latitude=glrLightPole.getLatitude();
        }else{
            return;
        }
        String longitude="";
        if(StringUtil.isNotEmpty(glrLightPole.getLongitude())){
            longitude=glrLightPole.getLongitude();
        }else{
            return;
        }

        String StUpdateSql="update glr_light_pole set latitude='"+latitude+"',longitude='"+longitude+"' where project_code='"+projectCode+"'";
        baseMapper.updateSql(StUpdateSql);
    }

    @Override
    public void deleteById(String id) throws Exception {
        GlrLightPole glrLightPole=baseMapper.selectById(id);
        if (GLRConstant.YN_Y.equals(glrLightPole.getLedYn())||
                GLRConstant.YN_Y.equals(glrLightPole.getLightYn())||
                GLRConstant.YN_Y.equals(glrLightPole.getRadioYn())||
                GLRConstant.YN_Y.equals(glrLightPole.getSensorYn())||
                GLRConstant.YN_Y.equals(glrLightPole.getVideoYn())||
                GLRConstant.YN_Y.equals(glrLightPole.getGatewayYn())){
            throw new Exception("此灯杆上还有别的设置，不能删除！");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public void deleteBatchByIds(List<String> asList) throws Exception {
        for (String stId : asList) {
            deleteById(stId);
        }
    }



}
